package com.eascs.crm.nsEditDetail.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

//import com.eascs.app.log.Logger;
//import com.eascs.app.log.LoggerFactory;
import com.eascs.crm.nsEditDetail.entity.ManagementInfoEntity;
import com.eascs.crm.nsEditDetail.service.ManagementInfoService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

@Controller
@RequestMapping("/managementInfo")
public class ManagementInfoRestController extends BaseRestController {
//	private final Logger logger = LoggerFactory.getLogger(ManagementInfoRestController.class);
	@Autowired
	private ManagementInfoService managementInfoService;

	// 新增修改
	@RequestMapping(value = "/update-index")
	public HtmlRestResult updateIndex(String loanapplyNo) {
		ManagementInfoEntity entity = managementInfoService.getByLoanNo(loanapplyNo);
		ViewContext.put("managementInfo", entity);
		ViewContext.put("loanapplyNo", loanapplyNo);
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/management_info_add");
	}

	@RequestMapping("/save")
	public BaseRestResult<String> assetsSave(ManagementInfoEntity form) {
		User loginUser = UserHolder.getUser();
		ManagementInfoEntity entity = managementInfoService.getByLoanNo(form.getLoanapplyNo());
		if (entity == null) {
			form.setCreateId(loginUser.getNo());
			form.setCreator(loginUser.getCname());
			form.setCreateDate(new Date());
			form.setModifyId(loginUser.getNo());
			form.setModifier(loginUser.getCname());
			form.setModifyDate(new Date());
			ManagementInfoEntity saveResult = managementInfoService.save(form);
			if (saveResult == null) {
				return BaseRestResult.info("修改失败");
			}
			return BaseRestResult.info("修改成功").andRefresh();
		} else {
				form.setCreateId(entity.getCreateId());
				form.setCreator(entity.getCreator());
				form.setCreateDate(entity.getCreateDate());
				form.setModifyId(loginUser.getNo());
				form.setModifier(loginUser.getCname());
				form.setModifyDate(new Date());
			int effectRow = managementInfoService.update(form);
			if (effectRow > 0) {
				return BaseRestResult.info("修改成功").andRefresh();
			} else {
				return BaseRestResult.info("修改失败");
			}
		}
	}
}
